<template><div><h2 id="一、优化" tabindex="-1"><a class="header-anchor" href="#一、优化" aria-hidden="true">#</a> 一、优化</h2>
<h3 id="_1、扩展序列化算法" tabindex="-1"><a class="header-anchor" href="#_1、扩展序列化算法" aria-hidden="true">#</a> 1、扩展序列化算法</h3>
<h4 id="_1、序列化接口" tabindex="-1"><a class="header-anchor" href="#_1、序列化接口" aria-hidden="true">#</a> 1、序列化接口</h4>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">Serializer</span> <span class="token punctuation">{</span>
    <span class="token doc-comment comment">/**
     * 序列化
     *
     * <span class="token keyword">@param</span> <span class="token parameter">object</span> 被序列化的对象
     * <span class="token keyword">@param</span> <span class="token class-name"><span class="token punctuation">&lt;</span>T<span class="token punctuation">></span></span>    被序列化对象类型
     * <span class="token keyword">@return</span> 序列化后的字节数组
     */</span>
    <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">></span></span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">serialize</span><span class="token punctuation">(</span><span class="token class-name">T</span> object<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token doc-comment comment">/**
     * 反序列化
     *
     * <span class="token keyword">@param</span> <span class="token parameter">clazz</span> 反序列化的目标类的Class对象
     * <span class="token keyword">@param</span> <span class="token parameter">bytes</span> 被反序列化的字节数组
     * <span class="token keyword">@param</span> <span class="token class-name"><span class="token punctuation">&lt;</span>T<span class="token punctuation">></span></span>   反序列化目标类
     * <span class="token keyword">@return</span> 反序列化后的对象
     */</span>
    <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">></span></span> <span class="token class-name">T</span> <span class="token function">deserialize</span><span class="token punctuation">(</span><span class="token class-name">Class</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">></span></span> clazz<span class="token punctuation">,</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_2、序列化接口的实现" tabindex="-1"><a class="header-anchor" href="#_2、序列化接口的实现" aria-hidden="true">#</a> 2、序列化接口的实现</h4>
<p>JDK原生序列化算法和Json序列化算法</p>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">enum</span> <span class="token class-name">SerializerAlgorithm</span> <span class="token keyword">implements</span> <span class="token class-name">Serializer</span> <span class="token punctuation">{</span>
    <span class="token comment">//Java的序列化及反序列方式</span>
    <span class="token class-name">Java</span> <span class="token punctuation">{</span>
        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">></span></span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">serialize</span><span class="token punctuation">(</span><span class="token class-name">T</span> object<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"使用Java方式序列化"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 序列化后的字节数组</span>
            <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
            <span class="token comment">//自动关闭资源</span>
            <span class="token keyword">try</span> <span class="token punctuation">(</span><span class="token class-name">ByteArrayOutputStream</span> bos <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ByteArrayOutputStream</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                 <span class="token class-name">ObjectOutputStream</span> oos <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectOutputStream</span><span class="token punctuation">(</span>bos<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">//将对象写入到输出流中</span>
                oos<span class="token punctuation">.</span><span class="token function">writeObject</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token comment">//转换为字节数组</span>
                bytes <span class="token operator">=</span> bos<span class="token punctuation">.</span><span class="token function">toByteArray</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token keyword">return</span> bytes<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">></span></span> <span class="token class-name">T</span> <span class="token function">deserialize</span><span class="token punctuation">(</span><span class="token class-name">Class</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">></span></span> clazz<span class="token punctuation">,</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"使用Java方式反序列化"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">T</span> target <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
            <span class="token keyword">try</span> <span class="token punctuation">(</span><span class="token class-name">ByteArrayInputStream</span> bis <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ByteArrayInputStream</span><span class="token punctuation">(</span>bytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
                 <span class="token class-name">ObjectInputStream</span> ois <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ObjectInputStream</span><span class="token punctuation">(</span>bis<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">//强转</span>
                target <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">T</span><span class="token punctuation">)</span> ois<span class="token punctuation">.</span><span class="token function">readObject</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> <span class="token operator">|</span> <span class="token class-name">ClassNotFoundException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token punctuation">}</span>
            <span class="token comment">// 返回反序列化后的对象</span>
            <span class="token keyword">return</span> target<span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span><span class="token punctuation">,</span>
    <span class="token comment">//Json的序列化的反序列化</span>
    <span class="token class-name">Json</span> <span class="token punctuation">{</span>
        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">></span></span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token function">serialize</span><span class="token punctuation">(</span><span class="token class-name">T</span> object<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"使用Json方式序列化"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">String</span> s <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Gson</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">toJson</span><span class="token punctuation">(</span>object<span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 指定字符集，获得字节数组</span>
            <span class="token keyword">return</span> s<span class="token punctuation">.</span><span class="token function">getBytes</span><span class="token punctuation">(</span><span class="token class-name">StandardCharsets</span><span class="token punctuation">.</span><span class="token constant">UTF_8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token annotation punctuation">@Override</span>
        <span class="token keyword">public</span> <span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">></span></span> <span class="token class-name">T</span> <span class="token function">deserialize</span><span class="token punctuation">(</span><span class="token class-name">Class</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">T</span><span class="token punctuation">></span></span> clazz<span class="token punctuation">,</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">"使用Json方式反序列化"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token class-name">String</span> s <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">String</span><span class="token punctuation">(</span>bytes<span class="token punctuation">,</span> <span class="token class-name">StandardCharsets</span><span class="token punctuation">.</span><span class="token constant">UTF_8</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            <span class="token comment">// 此处的clazz为具体类型的Class对象，而不是父类Message的</span>
            <span class="token keyword">return</span> <span class="token keyword">new</span> <span class="token class-name">Gson</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">fromJson</span><span class="token punctuation">(</span>s<span class="token punctuation">,</span> clazz<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_3、用户指定序列化方式" tabindex="-1"><a class="header-anchor" href="#_3、用户指定序列化方式" aria-hidden="true">#</a> 3、用户指定序列化方式</h4>
<p>从配置文件中获取</p>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">abstract</span> <span class="token keyword">class</span> <span class="token class-name">Config</span> <span class="token punctuation">{</span>
    <span class="token keyword">static</span> <span class="token class-name">Properties</span> properties<span class="token punctuation">;</span>

  <span class="token comment">//静态代码块</span>
    <span class="token keyword">static</span> <span class="token punctuation">{</span>
        <span class="token keyword">try</span> <span class="token punctuation">(</span><span class="token class-name">InputStream</span> in <span class="token operator">=</span> <span class="token class-name">Config</span><span class="token punctuation">.</span><span class="token keyword">class</span><span class="token punctuation">.</span><span class="token function">getResourceAsStream</span><span class="token punctuation">(</span><span class="token string">"/application.properties"</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            properties <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">Properties</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
            properties<span class="token punctuation">.</span><span class="token function">load</span><span class="token punctuation">(</span>in<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">IOException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">throw</span> <span class="token keyword">new</span> <span class="token class-name">ExceptionInInitializerError</span><span class="token punctuation">(</span>e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">int</span> <span class="token function">getServerPort</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">String</span> value <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"server.port"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>value <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token number">8080</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token keyword">return</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">parseInt</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">SerializerAlgorithm</span> <span class="token function">getSerializerAlgorithm</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token class-name">String</span> value <span class="token operator">=</span> properties<span class="token punctuation">.</span><span class="token function">getProperty</span><span class="token punctuation">(</span><span class="token string">"serializer.algorithm"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">if</span> <span class="token punctuation">(</span>value <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">//默认使用Java序列化方式</span>
            <span class="token keyword">return</span> <span class="token class-name">SerializerAlgorithm<span class="token punctuation">.</span>Java</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
            <span class="token comment">//使用配置文件指定的方式</span>
            <span class="token keyword">return</span> <span class="token class-name">SerializerAlgorithm</span><span class="token punctuation">.</span><span class="token function">valueOf</span><span class="token punctuation">(</span>value<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>
    <span class="token punctuation">}</span>


<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_4、改造编解码器" tabindex="-1"><a class="header-anchor" href="#_4、改造编解码器" aria-hidden="true">#</a> 4、改造编解码器</h4>
<p>使其可以按照指定的方式进行序列化及反序列化</p>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token annotation punctuation">@Slf4j</span>
<span class="token annotation punctuation">@ChannelHandler.Sharable</span>
<span class="token doc-comment comment">/**
 * 必须和 LengthFieldBasedFrameDecoder 一起使用，确保接到的 ByteBuf 消息是完整的
 */</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">MessageCodecSharable</span> <span class="token keyword">extends</span> <span class="token class-name">MessageToMessageCodec</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">ByteBuf</span><span class="token punctuation">,</span> <span class="token class-name">Message</span><span class="token punctuation">></span></span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">encode</span><span class="token punctuation">(</span><span class="token class-name">ChannelHandlerContext</span> ctx<span class="token punctuation">,</span> <span class="token class-name">Message</span> msg<span class="token punctuation">,</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">></span></span> outList<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
        <span class="token class-name">ByteBuf</span> out <span class="token operator">=</span> ctx<span class="token punctuation">.</span><span class="token function">alloc</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">buffer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 1. 4 字节的魔数</span>
        out<span class="token punctuation">.</span><span class="token function">writeBytes</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 2. 1 字节的版本,</span>
        out<span class="token punctuation">.</span><span class="token function">writeByte</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 3. 1 字节的序列化方式 jdk 0 , json 1</span>
      <span class="token comment">//重点改造</span>
        out<span class="token punctuation">.</span><span class="token function">writeByte</span><span class="token punctuation">(</span><span class="token class-name">Config</span><span class="token punctuation">.</span><span class="token function">getSerializerAlgorithm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">ordinal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 4. 1 字节的指令类型</span>
        out<span class="token punctuation">.</span><span class="token function">writeByte</span><span class="token punctuation">(</span>msg<span class="token punctuation">.</span><span class="token function">getMessageType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 5. 4 个字节</span>
        out<span class="token punctuation">.</span><span class="token function">writeInt</span><span class="token punctuation">(</span>msg<span class="token punctuation">.</span><span class="token function">getSequenceId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 无意义，对齐填充</span>
        out<span class="token punctuation">.</span><span class="token function">writeByte</span><span class="token punctuation">(</span><span class="token number">0xff</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 6. 获取内容的字节数组</span>
      <span class="token comment">//重点改造</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes <span class="token operator">=</span> <span class="token class-name">Config</span><span class="token punctuation">.</span><span class="token function">getSerializerAlgorithm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">serialize</span><span class="token punctuation">(</span>msg<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 7. 长度</span>
        out<span class="token punctuation">.</span><span class="token function">writeInt</span><span class="token punctuation">(</span>bytes<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 8. 写入内容</span>
        out<span class="token punctuation">.</span><span class="token function">writeBytes</span><span class="token punctuation">(</span>bytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        outList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>out<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">decode</span><span class="token punctuation">(</span><span class="token class-name">ChannelHandlerContext</span> ctx<span class="token punctuation">,</span> <span class="token class-name">ByteBuf</span> in<span class="token punctuation">,</span> <span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Object</span><span class="token punctuation">></span></span> out<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
        <span class="token keyword">int</span> magicNum <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">readInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span> version <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">readByte</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span> serializerType <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">readByte</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span> messageType <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">readByte</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> sequenceId <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">readInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        in<span class="token punctuation">.</span><span class="token function">readByte</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">int</span> length <span class="token operator">=</span> in<span class="token punctuation">.</span><span class="token function">readInt</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span>length<span class="token punctuation">]</span><span class="token punctuation">;</span>
        in<span class="token punctuation">.</span><span class="token function">readBytes</span><span class="token punctuation">(</span>bytes<span class="token punctuation">,</span> <span class="token number">0</span><span class="token punctuation">,</span> length<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//找到序列化算法</span>
        <span class="token class-name">SerializerAlgorithm</span> serializerAlgorithm <span class="token operator">=</span> <span class="token class-name">SerializerAlgorithm</span><span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span>serializerType<span class="token punctuation">]</span><span class="token punctuation">;</span>
        <span class="token comment">//确定具体的消息类型</span>
        <span class="token class-name">Class</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token operator">?</span><span class="token punctuation">></span></span> messageClass <span class="token operator">=</span> <span class="token class-name">Message</span><span class="token punctuation">.</span><span class="token function">getMessageClass</span><span class="token punctuation">(</span>messageType<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//反序列化</span>
        <span class="token class-name">Message</span> message <span class="token operator">=</span> <span class="token punctuation">(</span><span class="token class-name">Message</span><span class="token punctuation">)</span> serializerAlgorithm<span class="token punctuation">.</span><span class="token function">deserialize</span><span class="token punctuation">(</span>messageClass<span class="token punctuation">,</span> bytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
        log<span class="token punctuation">.</span><span class="token function">debug</span><span class="token punctuation">(</span><span class="token string">"{}"</span><span class="token punctuation">,</span> message<span class="token punctuation">)</span><span class="token punctuation">;</span>
        out<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>message<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_5、配置文件" tabindex="-1"><a class="header-anchor" href="#_5、配置文件" aria-hidden="true">#</a> 5、配置文件</h4>
<div class="language-properties line-numbers-mode" data-ext="properties"><pre v-pre class="language-properties"><code><span class="token key attr-name">serializer.algorithm</span><span class="token punctuation">=</span><span class="token value attr-value">Java</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div></div></div><h4 id="_6、测试代码" tabindex="-1"><a class="header-anchor" href="#_6、测试代码" aria-hidden="true">#</a> 6、测试代码</h4>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token annotation punctuation">@Test</span>
<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">testSerial</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token class-name">LoggingHandler</span> loggingHandler <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">LoggingHandler</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">MessageCodecSharable</span> messageCodecSharable <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">MessageCodecSharable</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token class-name">EmbeddedChannel</span> embeddedChannel <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">EmbeddedChannel</span><span class="token punctuation">(</span>loggingHandler<span class="token punctuation">,</span>messageCodecSharable<span class="token punctuation">,</span>loggingHandler<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//测试序列化及反序列化是否可配置</span>
    <span class="token comment">//登陆请求消息</span>
    <span class="token class-name">LoginRequestMessage</span> loginRequestMessage <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">LoginRequestMessage</span><span class="token punctuation">(</span><span class="token string">"zhangsan"</span><span class="token punctuation">,</span> <span class="token string">"123"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//出站->打印原始对象（loggingHandler）->编码为ByteBuf，此时会进行序列化（messageCodecSharable）->打印ByteBuf（loggingHandler）</span>
    embeddedChannel<span class="token punctuation">.</span><span class="token function">writeOutbound</span><span class="token punctuation">(</span>loginRequestMessage<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//入站->打印ByteBuf（loggingHandler）->解码为原始对象，此时会进行反序列化（messageCodecSharable）->打印原始对象（loggingHandler）</span>
    <span class="token comment">//构造一个ByteBuf</span>
    <span class="token class-name">ByteBuf</span> byteBuf <span class="token operator">=</span> <span class="token function">messageToByteBuf</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">LoginRequestMessage</span><span class="token punctuation">(</span><span class="token string">"lisi"</span><span class="token punctuation">,</span> <span class="token string">"3456"</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    embeddedChannel<span class="token punctuation">.</span><span class="token function">writeInbound</span><span class="token punctuation">(</span>byteBuf<span class="token punctuation">)</span><span class="token punctuation">;</span>


<span class="token punctuation">}</span>

<span class="token doc-comment comment">/**
 * @Description: 测试入站的时候，必须首先构造一个ByteBuf
 * @Author: Mr.Tong
 */</span>
<span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token class-name">ByteBuf</span> <span class="token function">messageToByteBuf</span><span class="token punctuation">(</span><span class="token class-name">Message</span> msg<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> algorithm <span class="token operator">=</span> <span class="token class-name">Config</span><span class="token punctuation">.</span><span class="token function">getSerializerAlgorithm</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">ordinal</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">//分配空间</span>
    <span class="token class-name">ByteBuf</span> out <span class="token operator">=</span> <span class="token class-name">ByteBufAllocator</span><span class="token punctuation">.</span><span class="token constant">DEFAULT</span><span class="token punctuation">.</span><span class="token function">buffer</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    out<span class="token punctuation">.</span><span class="token function">writeBytes</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">{</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">,</span> <span class="token number">3</span><span class="token punctuation">,</span> <span class="token number">4</span><span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    out<span class="token punctuation">.</span><span class="token function">writeByte</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    out<span class="token punctuation">.</span><span class="token function">writeByte</span><span class="token punctuation">(</span>algorithm<span class="token punctuation">)</span><span class="token punctuation">;</span>
    out<span class="token punctuation">.</span><span class="token function">writeByte</span><span class="token punctuation">(</span>msg<span class="token punctuation">.</span><span class="token function">getMessageType</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    out<span class="token punctuation">.</span><span class="token function">writeInt</span><span class="token punctuation">(</span>msg<span class="token punctuation">.</span><span class="token function">getSequenceId</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    out<span class="token punctuation">.</span><span class="token function">writeByte</span><span class="token punctuation">(</span><span class="token number">0xff</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">byte</span><span class="token punctuation">[</span><span class="token punctuation">]</span> bytes <span class="token operator">=</span> <span class="token class-name">SerializerAlgorithm</span><span class="token punctuation">.</span><span class="token function">values</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">[</span>algorithm<span class="token punctuation">]</span><span class="token punctuation">.</span><span class="token function">serialize</span><span class="token punctuation">(</span>msg<span class="token punctuation">)</span><span class="token punctuation">;</span>
    out<span class="token punctuation">.</span><span class="token function">writeInt</span><span class="token punctuation">(</span>bytes<span class="token punctuation">.</span>length<span class="token punctuation">)</span><span class="token punctuation">;</span>
    out<span class="token punctuation">.</span><span class="token function">writeBytes</span><span class="token punctuation">(</span>bytes<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> out<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h4 id="_7、运行结果" tabindex="-1"><a class="header-anchor" href="#_7、运行结果" aria-hidden="true">#</a> 7、运行结果</h4>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token comment">//测试出站-log首先打印原始对象</span>
<span class="token number">23</span><span class="token operator">:</span><span class="token number">33</span><span class="token operator">:</span><span class="token number">12</span> <span class="token punctuation">[</span><span class="token constant">DEBUG</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>main<span class="token punctuation">]</span> <span class="token class-name"><span class="token namespace">i<span class="token punctuation">.</span>n<span class="token punctuation">.</span>h<span class="token punctuation">.</span>l<span class="token punctuation">.</span></span>LoggingHandler</span> <span class="token operator">-</span> <span class="token punctuation">[</span>id<span class="token operator">:</span> <span class="token number">0</span>xembedded<span class="token punctuation">,</span> <span class="token class-name">L</span><span class="token operator">:</span>embedded <span class="token operator">-</span> <span class="token class-name">R</span><span class="token operator">:</span>embedded<span class="token punctuation">]</span> <span class="token constant">WRITE</span><span class="token operator">:</span> <span class="token class-name">LoginRequestMessage</span><span class="token punctuation">(</span><span class="token keyword">super</span><span class="token operator">=</span><span class="token class-name">Message</span><span class="token punctuation">(</span>sequenceId<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> messageType<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> username<span class="token operator">=</span>zhangsan<span class="token punctuation">,</span> password<span class="token operator">=</span><span class="token number">123</span><span class="token punctuation">)</span>
  <span class="token comment">//编码</span>
使用<span class="token class-name">Java</span>方式序列化
  <span class="token comment">//log打印编码后的ByteBuf</span>
<span class="token number">23</span><span class="token operator">:</span><span class="token number">33</span><span class="token operator">:</span><span class="token number">12</span> <span class="token punctuation">[</span><span class="token constant">DEBUG</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>main<span class="token punctuation">]</span> <span class="token class-name"><span class="token namespace">i<span class="token punctuation">.</span>n<span class="token punctuation">.</span>h<span class="token punctuation">.</span>l<span class="token punctuation">.</span></span>LoggingHandler</span> <span class="token operator">-</span> <span class="token punctuation">[</span>id<span class="token operator">:</span> <span class="token number">0</span>xembedded<span class="token punctuation">,</span> <span class="token class-name">L</span><span class="token operator">:</span>embedded <span class="token operator">-</span> <span class="token class-name">R</span><span class="token operator">:</span>embedded<span class="token punctuation">]</span> <span class="token constant">WRITE</span><span class="token operator">:</span> <span class="token number">214</span>B
         <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span>
         <span class="token operator">|</span>  <span class="token number">0</span>  <span class="token number">1</span>  <span class="token number">2</span>  <span class="token number">3</span>  <span class="token number">4</span>  <span class="token number">5</span>  <span class="token number">6</span>  <span class="token number">7</span>  <span class="token number">8</span>  <span class="token number">9</span>  a  b  c  d  e  f <span class="token operator">|</span>
<span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span>
<span class="token operator">|</span><span class="token number">00000000</span><span class="token operator">|</span> <span class="token number">01</span> <span class="token number">02</span> <span class="token number">03</span> <span class="token number">04</span> <span class="token number">01</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> ff <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> c6 <span class="token operator">|</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>|
<span class="token operator">|</span><span class="token number">00000010</span><span class="token operator">|</span> ac ed <span class="token number">00</span> <span class="token number">05</span> <span class="token number">73</span> <span class="token number">72</span> <span class="token number">00</span> <span class="token number">25</span> <span class="token number">63</span> <span class="token number">6</span>e <span class="token number">2</span>e <span class="token number">69</span> <span class="token number">74</span> <span class="token number">63</span> <span class="token number">61</span> <span class="token number">73</span> <span class="token operator">|</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>sr<span class="token punctuation">.</span>%cn<span class="token punctuation">.</span>itcas<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000020</span><span class="token operator">|</span> <span class="token number">74</span> <span class="token number">2</span>e <span class="token number">6d</span> <span class="token number">65</span> <span class="token number">73</span> <span class="token number">73</span> <span class="token number">61</span> <span class="token number">67</span> <span class="token number">65</span> <span class="token number">2</span>e <span class="token number">4</span>c <span class="token number">6f</span> <span class="token number">67</span> <span class="token number">69</span> <span class="token number">6</span>e <span class="token number">52</span> <span class="token operator">|</span><span class="token class-name"><span class="token namespace">t<span class="token punctuation">.</span>message<span class="token punctuation">.</span></span>LoginR</span><span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000030</span><span class="token operator">|</span> <span class="token number">65</span> <span class="token number">71</span> <span class="token number">75</span> <span class="token number">65</span> <span class="token number">73</span> <span class="token number">74</span> <span class="token number">4d</span> <span class="token number">65</span> <span class="token number">73</span> <span class="token number">73</span> <span class="token number">61</span> <span class="token number">67</span> <span class="token number">65</span> a0 <span class="token number">3f</span> <span class="token number">71</span> <span class="token operator">|</span>equestMessage<span class="token punctuation">.</span>?q<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000040</span><span class="token operator">|</span> cb <span class="token number">31</span> <span class="token number">45</span> b5 <span class="token number">88</span> <span class="token number">02</span> <span class="token number">00</span> <span class="token number">02</span> <span class="token number">4</span>c <span class="token number">00</span> <span class="token number">08</span> <span class="token number">70</span> <span class="token number">61</span> <span class="token number">73</span> <span class="token number">73</span> <span class="token number">77</span> <span class="token operator">|</span><span class="token number">.1</span>E<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>L<span class="token punctuation">.</span><span class="token punctuation">.</span>passw<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000050</span><span class="token operator">|</span> <span class="token number">6f</span> <span class="token number">72</span> <span class="token number">64</span> <span class="token number">74</span> <span class="token number">00</span> <span class="token number">12</span> <span class="token number">4</span>c <span class="token number">6</span>a <span class="token number">61</span> <span class="token number">76</span> <span class="token number">61</span> <span class="token number">2f</span> <span class="token number">6</span>c <span class="token number">61</span> <span class="token number">6</span>e <span class="token number">67</span> <span class="token operator">|</span>ordt<span class="token punctuation">.</span><span class="token punctuation">.</span>Ljava<span class="token operator">/</span>lang<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000060</span><span class="token operator">|</span> <span class="token number">2f</span> <span class="token number">53</span> <span class="token number">74</span> <span class="token number">72</span> <span class="token number">69</span> <span class="token number">6</span>e <span class="token number">67</span> <span class="token number">3</span>b <span class="token number">4</span>c <span class="token number">00</span> <span class="token number">08</span> <span class="token number">75</span> <span class="token number">73</span> <span class="token number">65</span> <span class="token number">72</span> <span class="token number">6</span>e <span class="token operator">|</span><span class="token operator">/</span><span class="token class-name">String</span><span class="token punctuation">;</span><span class="token class-name">L</span><span class="token punctuation">.</span><span class="token punctuation">.</span>usern<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000070</span><span class="token operator">|</span> <span class="token number">61</span> <span class="token number">6d</span> <span class="token number">65</span> <span class="token number">71</span> <span class="token number">00</span> <span class="token number">7</span>e <span class="token number">00</span> <span class="token number">01</span> <span class="token number">78</span> <span class="token number">72</span> <span class="token number">00</span> <span class="token number">19</span> <span class="token number">63</span> <span class="token number">6</span>e <span class="token number">2</span>e <span class="token number">69</span> <span class="token operator">|</span>ameq<span class="token punctuation">.</span>~<span class="token punctuation">.</span><span class="token punctuation">.</span>xr<span class="token punctuation">.</span><span class="token punctuation">.</span>cn<span class="token punctuation">.</span>i<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000080</span><span class="token operator">|</span> <span class="token number">74</span> <span class="token number">63</span> <span class="token number">61</span> <span class="token number">73</span> <span class="token number">74</span> <span class="token number">2</span>e <span class="token number">6d</span> <span class="token number">65</span> <span class="token number">73</span> <span class="token number">73</span> <span class="token number">61</span> <span class="token number">67</span> <span class="token number">65</span> <span class="token number">2</span>e <span class="token number">4d</span> <span class="token number">65</span> <span class="token operator">|</span><span class="token class-name"><span class="token namespace">tcast<span class="token punctuation">.</span>message<span class="token punctuation">.</span></span>Me</span><span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000090</span><span class="token operator">|</span> <span class="token number">73</span> <span class="token number">73</span> <span class="token number">61</span> <span class="token number">67</span> <span class="token number">65</span> <span class="token number">0</span>b <span class="token number">0</span>b f8 b3 <span class="token number">48</span> <span class="token number">3</span>e <span class="token number">94</span> <span class="token number">55</span> <span class="token number">02</span> <span class="token number">00</span> <span class="token number">02</span> <span class="token operator">|</span>ssage<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>H<span class="token operator">></span><span class="token punctuation">.</span>U<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>|
<span class="token operator">|</span><span class="token number">000000</span>a0<span class="token operator">|</span> <span class="token number">49</span> <span class="token number">00</span> <span class="token number">0</span>b <span class="token number">6d</span> <span class="token number">65</span> <span class="token number">73</span> <span class="token number">73</span> <span class="token number">61</span> <span class="token number">67</span> <span class="token number">65</span> <span class="token number">54</span> <span class="token number">79</span> <span class="token number">70</span> <span class="token number">65</span> <span class="token number">49</span> <span class="token number">00</span> <span class="token operator">|</span><span class="token class-name">I</span><span class="token punctuation">.</span><span class="token punctuation">.</span>messageTypeI<span class="token punctuation">.</span>|
<span class="token operator">|</span><span class="token number">000000</span>b0<span class="token operator">|</span> <span class="token number">0</span>a <span class="token number">73</span> <span class="token number">65</span> <span class="token number">71</span> <span class="token number">75</span> <span class="token number">65</span> <span class="token number">6</span>e <span class="token number">63</span> <span class="token number">65</span> <span class="token number">49</span> <span class="token number">64</span> <span class="token number">78</span> <span class="token number">70</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token operator">|</span><span class="token punctuation">.</span>sequenceIdxp<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>|
<span class="token operator">|</span><span class="token number">000000</span>c0<span class="token operator">|</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">74</span> <span class="token number">00</span> <span class="token number">03</span> <span class="token number">31</span> <span class="token number">32</span> <span class="token number">33</span> <span class="token number">74</span> <span class="token number">00</span> <span class="token number">08</span> <span class="token number">7</span>a <span class="token number">68</span> <span class="token operator">|</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>t<span class="token punctuation">.</span><span class="token number">.123</span>t<span class="token punctuation">.</span><span class="token punctuation">.</span>zh<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">000000d</span><span class="token number">0</span><span class="token operator">|</span> <span class="token number">61</span> <span class="token number">6</span>e <span class="token number">67</span> <span class="token number">73</span> <span class="token number">61</span> <span class="token number">6</span>e                               <span class="token operator">|</span>angsan          <span class="token operator">|</span>
<span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span>
<span class="token number">23</span><span class="token operator">:</span><span class="token number">33</span><span class="token operator">:</span><span class="token number">12</span> <span class="token punctuation">[</span><span class="token constant">DEBUG</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>main<span class="token punctuation">]</span> <span class="token class-name"><span class="token namespace">i<span class="token punctuation">.</span>n<span class="token punctuation">.</span>h<span class="token punctuation">.</span>l<span class="token punctuation">.</span></span>LoggingHandler</span> <span class="token operator">-</span> <span class="token punctuation">[</span>id<span class="token operator">:</span> <span class="token number">0</span>xembedded<span class="token punctuation">,</span> <span class="token class-name">L</span><span class="token operator">:</span>embedded <span class="token operator">-</span> <span class="token class-name">R</span><span class="token operator">:</span>embedded<span class="token punctuation">]</span> <span class="token constant">FLUSH</span>
<span class="token number">23</span><span class="token operator">:</span><span class="token number">33</span><span class="token operator">:</span><span class="token number">12</span> <span class="token punctuation">[</span><span class="token constant">DEBUG</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>main<span class="token punctuation">]</span> <span class="token class-name"><span class="token namespace">i<span class="token punctuation">.</span>n<span class="token punctuation">.</span>h<span class="token punctuation">.</span>l<span class="token punctuation">.</span></span>LoggingHandler</span> <span class="token operator">-</span> <span class="token punctuation">[</span>id<span class="token operator">:</span> <span class="token number">0</span>xembedded<span class="token punctuation">,</span> <span class="token class-name">L</span><span class="token operator">:</span>embedded <span class="token operator">-</span> <span class="token class-name">R</span><span class="token operator">:</span>embedded<span class="token punctuation">]</span> <span class="token constant">FLUSH</span>
  <span class="token comment">//测试入站时候构造ByteBuf，此时会调用一次序列化过程</span>
使用<span class="token class-name">Java</span>方式序列化
  <span class="token comment">//log首先打印ByteBuf数据</span>
<span class="token number">23</span><span class="token operator">:</span><span class="token number">33</span><span class="token operator">:</span><span class="token number">12</span> <span class="token punctuation">[</span><span class="token constant">DEBUG</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>main<span class="token punctuation">]</span> <span class="token class-name"><span class="token namespace">i<span class="token punctuation">.</span>n<span class="token punctuation">.</span>h<span class="token punctuation">.</span>l<span class="token punctuation">.</span></span>LoggingHandler</span> <span class="token operator">-</span> <span class="token punctuation">[</span>id<span class="token operator">:</span> <span class="token number">0</span>xembedded<span class="token punctuation">,</span> <span class="token class-name">L</span><span class="token operator">:</span>embedded <span class="token operator">-</span> <span class="token class-name">R</span><span class="token operator">:</span>embedded<span class="token punctuation">]</span> <span class="token constant">READ</span><span class="token operator">:</span> <span class="token number">211</span>B
         <span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span>
         <span class="token operator">|</span>  <span class="token number">0</span>  <span class="token number">1</span>  <span class="token number">2</span>  <span class="token number">3</span>  <span class="token number">4</span>  <span class="token number">5</span>  <span class="token number">6</span>  <span class="token number">7</span>  <span class="token number">8</span>  <span class="token number">9</span>  a  b  c  d  e  f <span class="token operator">|</span>
<span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span>
<span class="token operator">|</span><span class="token number">00000000</span><span class="token operator">|</span> <span class="token number">01</span> <span class="token number">02</span> <span class="token number">03</span> <span class="token number">04</span> <span class="token number">01</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> ff <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> c3 <span class="token operator">|</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>|
<span class="token operator">|</span><span class="token number">00000010</span><span class="token operator">|</span> ac ed <span class="token number">00</span> <span class="token number">05</span> <span class="token number">73</span> <span class="token number">72</span> <span class="token number">00</span> <span class="token number">25</span> <span class="token number">63</span> <span class="token number">6</span>e <span class="token number">2</span>e <span class="token number">69</span> <span class="token number">74</span> <span class="token number">63</span> <span class="token number">61</span> <span class="token number">73</span> <span class="token operator">|</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>sr<span class="token punctuation">.</span>%cn<span class="token punctuation">.</span>itcas<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000020</span><span class="token operator">|</span> <span class="token number">74</span> <span class="token number">2</span>e <span class="token number">6d</span> <span class="token number">65</span> <span class="token number">73</span> <span class="token number">73</span> <span class="token number">61</span> <span class="token number">67</span> <span class="token number">65</span> <span class="token number">2</span>e <span class="token number">4</span>c <span class="token number">6f</span> <span class="token number">67</span> <span class="token number">69</span> <span class="token number">6</span>e <span class="token number">52</span> <span class="token operator">|</span><span class="token class-name"><span class="token namespace">t<span class="token punctuation">.</span>message<span class="token punctuation">.</span></span>LoginR</span><span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000030</span><span class="token operator">|</span> <span class="token number">65</span> <span class="token number">71</span> <span class="token number">75</span> <span class="token number">65</span> <span class="token number">73</span> <span class="token number">74</span> <span class="token number">4d</span> <span class="token number">65</span> <span class="token number">73</span> <span class="token number">73</span> <span class="token number">61</span> <span class="token number">67</span> <span class="token number">65</span> a0 <span class="token number">3f</span> <span class="token number">71</span> <span class="token operator">|</span>equestMessage<span class="token punctuation">.</span>?q<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000040</span><span class="token operator">|</span> cb <span class="token number">31</span> <span class="token number">45</span> b5 <span class="token number">88</span> <span class="token number">02</span> <span class="token number">00</span> <span class="token number">02</span> <span class="token number">4</span>c <span class="token number">00</span> <span class="token number">08</span> <span class="token number">70</span> <span class="token number">61</span> <span class="token number">73</span> <span class="token number">73</span> <span class="token number">77</span> <span class="token operator">|</span><span class="token number">.1</span>E<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>L<span class="token punctuation">.</span><span class="token punctuation">.</span>passw<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000050</span><span class="token operator">|</span> <span class="token number">6f</span> <span class="token number">72</span> <span class="token number">64</span> <span class="token number">74</span> <span class="token number">00</span> <span class="token number">12</span> <span class="token number">4</span>c <span class="token number">6</span>a <span class="token number">61</span> <span class="token number">76</span> <span class="token number">61</span> <span class="token number">2f</span> <span class="token number">6</span>c <span class="token number">61</span> <span class="token number">6</span>e <span class="token number">67</span> <span class="token operator">|</span>ordt<span class="token punctuation">.</span><span class="token punctuation">.</span>Ljava<span class="token operator">/</span>lang<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000060</span><span class="token operator">|</span> <span class="token number">2f</span> <span class="token number">53</span> <span class="token number">74</span> <span class="token number">72</span> <span class="token number">69</span> <span class="token number">6</span>e <span class="token number">67</span> <span class="token number">3</span>b <span class="token number">4</span>c <span class="token number">00</span> <span class="token number">08</span> <span class="token number">75</span> <span class="token number">73</span> <span class="token number">65</span> <span class="token number">72</span> <span class="token number">6</span>e <span class="token operator">|</span><span class="token operator">/</span><span class="token class-name">String</span><span class="token punctuation">;</span><span class="token class-name">L</span><span class="token punctuation">.</span><span class="token punctuation">.</span>usern<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000070</span><span class="token operator">|</span> <span class="token number">61</span> <span class="token number">6d</span> <span class="token number">65</span> <span class="token number">71</span> <span class="token number">00</span> <span class="token number">7</span>e <span class="token number">00</span> <span class="token number">01</span> <span class="token number">78</span> <span class="token number">72</span> <span class="token number">00</span> <span class="token number">19</span> <span class="token number">63</span> <span class="token number">6</span>e <span class="token number">2</span>e <span class="token number">69</span> <span class="token operator">|</span>ameq<span class="token punctuation">.</span>~<span class="token punctuation">.</span><span class="token punctuation">.</span>xr<span class="token punctuation">.</span><span class="token punctuation">.</span>cn<span class="token punctuation">.</span>i<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000080</span><span class="token operator">|</span> <span class="token number">74</span> <span class="token number">63</span> <span class="token number">61</span> <span class="token number">73</span> <span class="token number">74</span> <span class="token number">2</span>e <span class="token number">6d</span> <span class="token number">65</span> <span class="token number">73</span> <span class="token number">73</span> <span class="token number">61</span> <span class="token number">67</span> <span class="token number">65</span> <span class="token number">2</span>e <span class="token number">4d</span> <span class="token number">65</span> <span class="token operator">|</span><span class="token class-name"><span class="token namespace">tcast<span class="token punctuation">.</span>message<span class="token punctuation">.</span></span>Me</span><span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">00000090</span><span class="token operator">|</span> <span class="token number">73</span> <span class="token number">73</span> <span class="token number">61</span> <span class="token number">67</span> <span class="token number">65</span> <span class="token number">0</span>b <span class="token number">0</span>b f8 b3 <span class="token number">48</span> <span class="token number">3</span>e <span class="token number">94</span> <span class="token number">55</span> <span class="token number">02</span> <span class="token number">00</span> <span class="token number">02</span> <span class="token operator">|</span>ssage<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>H<span class="token operator">></span><span class="token punctuation">.</span>U<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>|
<span class="token operator">|</span><span class="token number">000000</span>a0<span class="token operator">|</span> <span class="token number">49</span> <span class="token number">00</span> <span class="token number">0</span>b <span class="token number">6d</span> <span class="token number">65</span> <span class="token number">73</span> <span class="token number">73</span> <span class="token number">61</span> <span class="token number">67</span> <span class="token number">65</span> <span class="token number">54</span> <span class="token number">79</span> <span class="token number">70</span> <span class="token number">65</span> <span class="token number">49</span> <span class="token number">00</span> <span class="token operator">|</span><span class="token class-name">I</span><span class="token punctuation">.</span><span class="token punctuation">.</span>messageTypeI<span class="token punctuation">.</span>|
<span class="token operator">|</span><span class="token number">000000</span>b0<span class="token operator">|</span> <span class="token number">0</span>a <span class="token number">73</span> <span class="token number">65</span> <span class="token number">71</span> <span class="token number">75</span> <span class="token number">65</span> <span class="token number">6</span>e <span class="token number">63</span> <span class="token number">65</span> <span class="token number">49</span> <span class="token number">64</span> <span class="token number">78</span> <span class="token number">70</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token operator">|</span><span class="token punctuation">.</span>sequenceIdxp<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>|
<span class="token operator">|</span><span class="token number">000000</span>c0<span class="token operator">|</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">00</span> <span class="token number">74</span> <span class="token number">00</span> <span class="token number">04</span> <span class="token number">33</span> <span class="token number">34</span> <span class="token number">35</span> <span class="token number">36</span> <span class="token number">74</span> <span class="token number">00</span> <span class="token number">04</span> <span class="token number">6</span>c <span class="token operator">|</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>t<span class="token punctuation">.</span><span class="token number">.3456</span>t<span class="token punctuation">.</span><span class="token punctuation">.</span>l<span class="token operator">|</span>
<span class="token operator">|</span><span class="token number">000000d</span><span class="token number">0</span><span class="token operator">|</span> <span class="token number">69</span> <span class="token number">73</span> <span class="token number">69</span>                                        <span class="token operator">|</span>isi             <span class="token operator">|</span>
<span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">-</span><span class="token operator">+</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">--</span><span class="token operator">+</span>
  <span class="token comment">//进行反序列化</span>
使用<span class="token class-name">Java</span>方式反序列化
<span class="token number">23</span><span class="token operator">:</span><span class="token number">33</span><span class="token operator">:</span><span class="token number">12</span> <span class="token punctuation">[</span><span class="token constant">DEBUG</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>main<span class="token punctuation">]</span> <span class="token class-name"><span class="token namespace">c<span class="token punctuation">.</span>i<span class="token punctuation">.</span>p<span class="token punctuation">.</span></span>MessageCodecSharable</span> <span class="token operator">-</span> <span class="token class-name">LoginRequestMessage</span><span class="token punctuation">(</span><span class="token keyword">super</span><span class="token operator">=</span><span class="token class-name">Message</span><span class="token punctuation">(</span>sequenceId<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> messageType<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> username<span class="token operator">=</span>lisi<span class="token punctuation">,</span> password<span class="token operator">=</span><span class="token number">3456</span><span class="token punctuation">)</span>
  <span class="token comment">//log打印原始对象</span>
<span class="token number">23</span><span class="token operator">:</span><span class="token number">33</span><span class="token operator">:</span><span class="token number">12</span> <span class="token punctuation">[</span><span class="token constant">DEBUG</span><span class="token punctuation">]</span> <span class="token punctuation">[</span>main<span class="token punctuation">]</span> <span class="token class-name"><span class="token namespace">i<span class="token punctuation">.</span>n<span class="token punctuation">.</span>h<span class="token punctuation">.</span>l<span class="token punctuation">.</span></span>LoggingHandler</span> <span class="token operator">-</span> <span class="token punctuation">[</span>id<span class="token operator">:</span> <span class="token number">0</span>xembedded<span class="token punctuation">,</span> <span class="token class-name">L</span><span class="token operator">:</span>embedded <span class="token operator">-</span> <span class="token class-name">R</span><span class="token operator">:</span>embedded<span class="token punctuation">]</span> <span class="token constant">READ</span><span class="token operator">:</span> <span class="token class-name">LoginRequestMessage</span><span class="token punctuation">(</span><span class="token keyword">super</span><span class="token operator">=</span><span class="token class-name">Message</span><span class="token punctuation">(</span>sequenceId<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">,</span> messageType<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">,</span> username<span class="token operator">=</span>lisi<span class="token punctuation">,</span> password<span class="token operator">=</span><span class="token number">3456</span><span class="token punctuation">)</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><h3 id="_2、参数调优" tabindex="-1"><a class="header-anchor" href="#_2、参数调优" aria-hidden="true">#</a> 2、参数调优</h3>
<p><strong><code v-pre>CONNECT_TIMEOUT_MILLIS</code></strong></p>
<ul>
<li>属于 <code v-pre>SocketChannal</code> 的参数</li>
<li>用在客户端建立连接时，如果在指定毫秒内无法连接，会抛出 <code v-pre>timeout</code> 异常，这个<code v-pre>timeout</code>异常是<code v-pre>Netty</code>中的异常</li>
<li>参数应该设置多大？如果太小的话，由于网络情况不是很好，那么还没来得及连接，就直接抛出<code v-pre>timeout</code>异常了，所以我们设置的大一点，保证客户端有足够的时间去尝试连接服务器</li>
<li>时间设置太大的话，如果服务器本来就是不可用的话，那么客户端不会等到设置的时间再抛出<code v-pre>timeout</code>异常，会检测服务器不可用之后，直接抛出更底层的<code v-pre>java.net.ConnectException</code>异常，客户端会<code v-pre>catch</code>该异常，然后抛出，所以不会抛出<code v-pre>timeout</code>异常。</li>
<li>所以如果想处理该异常，不要只<code v-pre>catch</code> <code v-pre>timeout</code>异常或者是<code v-pre>ConnectException</code>异常，应该直接<code v-pre>catch</code> <code v-pre>Exception</code>异常。</li>
<li>注意：<code v-pre>Netty</code> 中不要用成了<code v-pre>SO_TIMEOUT</code>，<code v-pre>SO_TIMEOUT</code>主要用在阻塞 <code v-pre>IO</code>，而 <code v-pre>Netty</code> 是非阻塞 <code v-pre>IO</code></li>
</ul>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">TestParam</span> <span class="token punctuation">{</span>
    <span class="token keyword">public</span> <span class="token keyword">static</span> <span class="token keyword">void</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token class-name">String</span><span class="token punctuation">[</span><span class="token punctuation">]</span> args<span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// 客户端</span>
        <span class="token comment">// SocketChannel 5s内未建立连接就抛出异常</span>
        <span class="token keyword">new</span> <span class="token class-name">Bootstrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token class-name">ChannelOption</span><span class="token punctuation">.</span><span class="token constant">CONNECT_TIMEOUT_MILLIS</span><span class="token punctuation">,</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
      <span class="token comment">// 服务器端</span>
        <span class="token comment">// ServerSocketChannel 5s内未建立连接就抛出异常</span>
        <span class="token keyword">new</span> <span class="token class-name">ServerBootstrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token class-name">ChannelOption</span><span class="token punctuation">.</span><span class="token constant">CONNECT_TIMEOUT_MILLIS</span><span class="token punctuation">,</span><span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// SocketChannel 5s内未建立连接就抛出异常</span>
        <span class="token keyword">new</span> <span class="token class-name">ServerBootstrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">childOption</span><span class="token punctuation">(</span><span class="token class-name">ChannelOption</span><span class="token punctuation">.</span><span class="token constant">CONNECT_TIMEOUT_MILLIS</span><span class="token punctuation">,</span> <span class="token number">5000</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><ul>
<li>客户端通过 <code v-pre>Bootstrap.option</code> 函数来配置参数，配置参数作用于 <code v-pre>SocketChannel</code></li>
<li>服务器通过<code v-pre>ServerBootstrap</code>来配置参数，但是对于不同的 <code v-pre>Channel</code> 需要选择不同的方法
<ul>
<li>通过 <code v-pre>option</code> 来配置 <code v-pre>ServerSocketChannel</code> 上的参数</li>
<li>通过 <code v-pre>childOption</code> 来配置 <code v-pre>SocketChannel</code> 上的参数</li>
</ul>
</li>
</ul>
<ul>
<li><strong>源码分析</strong></li>
</ul>
<p>客户端中连接服务器的线程是 <code v-pre>NIO</code> 线程，抛出异常的是主线程。这是如何做到超时判断以及线程通信的呢？</p>
<p>在<code v-pre>AbstractNioChannel.AbstractNioUnsafe.connect</code>方法中：</p>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">final</span> <span class="token keyword">void</span> <span class="token function">connect</span><span class="token punctuation">(</span>
                <span class="token keyword">final</span> <span class="token class-name">SocketAddress</span> remoteAddress<span class="token punctuation">,</span> <span class="token keyword">final</span> <span class="token class-name">SocketAddress</span> localAddress<span class="token punctuation">,</span> <span class="token keyword">final</span> <span class="token class-name">ChannelPromise</span> promise<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    
    <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
        
    <span class="token comment">// Schedule connect timeout.</span>
    <span class="token comment">// 设置超时时间，通过option方法传入的CONNECT_TIMEOUT_MILLIS参数进行设置</span>
    <span class="token keyword">int</span> connectTimeoutMillis <span class="token operator">=</span> <span class="token function">config</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">getConnectTimeoutMillis</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token comment">// 如果超时时间大于0</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span>connectTimeoutMillis <span class="token operator">></span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// 创建一个定时任务，延时connectTimeoutMillis（设置的超时时间时间）后执行</span>
        <span class="token comment">// schedule(Runnable command, long delay, TimeUnit unit)</span>
        connectTimeoutFuture <span class="token operator">=</span> <span class="token function">eventLoop</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">schedule</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">Runnable</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token annotation punctuation">@Override</span>
            <span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token comment">// 判断是否建立连接，Promise进行NIO线程与主线程之间的通信</span>
                <span class="token comment">// 如果超时，则通过tryFailure方法将异常放入Promise中</span>
                <span class="token comment">// 在主线程中抛出</span>
                <span class="token class-name">ChannelPromise</span> connectPromise <span class="token operator">=</span> <span class="token class-name">AbstractNioChannel</span><span class="token punctuation">.</span><span class="token keyword">this</span><span class="token punctuation">.</span>connectPromise<span class="token punctuation">;</span><span class="token comment">// 这个和主线程的future是同一个对象</span>
                <span class="token class-name">ConnectTimeoutException</span> cause <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">ConnectTimeoutException</span><span class="token punctuation">(</span><span class="token string">"connection timed out: "</span> <span class="token operator">+</span> remoteAddress<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>connectPromise <span class="token operator">!=</span> <span class="token keyword">null</span> <span class="token operator">&amp;&amp;</span> connectPromise<span class="token punctuation">.</span><span class="token function">tryFailure</span><span class="token punctuation">(</span>cause<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token function">close</span><span class="token punctuation">(</span><span class="token function">voidPromise</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span><span class="token punctuation">,</span> connectTimeoutMillis<span class="token punctuation">,</span> <span class="token class-name">TimeUnit</span><span class="token punctuation">.</span><span class="token constant">MILLISECONDS</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    
   	<span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
        
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>超时的判断主要是通过 <code v-pre>Eventloop</code> 的 <code v-pre>schedule</code> 方法和 <code v-pre>Promise</code> 共同实现的</p>
<ul>
<li><code v-pre>schedule</code> 设置了一个定时任务，延迟<code v-pre>connectTimeoutMillis</code>秒后执行该方法</li>
<li>如果指定时间内没有建立连接，则会执行其中的任务
<ul>
<li>任务负责创建 <code v-pre>ConnectTimeoutException</code> 异常，并将异常通过 <code v-pre>Pormise</code> 传给主线程并抛出</li>
</ul>
</li>
</ul>
<p><strong><code v-pre>SO_BACKLOG</code></strong></p>
<ul>
<li>
<p>该参数是 <code v-pre>ServerSocketChannel</code> 的参数</p>
</li>
<li>
<p><strong>三次握手与连接队列</strong></p>
</li>
</ul>
<p>第一次握手时，因为客户端与服务器之间的连接还未完全建立，连接会被放入半连接队列中</p>
<p>当完成三次握手以后，连接会被放入全连接队列中</p>
<p>服务器处理<code v-pre>Accept</code>事件是在<code v-pre>TCP</code>三次握手，也就是建立连接之后。服务器会从全连接队列中获取连接并进行处理</p>
<p>三次握手的完整图如下：</p>
<p>在 <code v-pre>linux 2.2</code> 之前，<code v-pre>backlog</code> 大小包括了两个队列的大小，在 <code v-pre>linux 2.2</code> 之后，分别用下面两个参数来控制</p>
<ul>
<li>半连接队列 - <code v-pre>sync queue</code>
<ul>
<li>大小通过 <code v-pre>/proc/sys/net/ipv4/tcp_max_syn_backlog</code> 指定，在 <code v-pre>syncookies</code> 启用的情况下，逻辑上没有最大值限制，这个设置便被忽略</li>
</ul>
</li>
<li>全连接队列 - <code v-pre>accept queue</code>
<ul>
<li>其大小通过 <code v-pre>/proc/sys/net/core/somaxconn</code> 指定，在使用 <code v-pre>listen</code> 函数时，内核会根据传入的 <code v-pre>backlog</code> 参数与系统参数，取二者的较小值</li>
<li>如果 <code v-pre>accpet queue</code> 队列满了，<code v-pre>server</code> 将发送一个拒绝连接的错误信息到 <code v-pre>client</code></li>
</ul>
</li>
<li>参考
<ul>
<li><a href="https://juejin.cn/post/7157182123441389604" target="_blank" rel="noopener noreferrer">https://juejin.cn/post/7157182123441389604<ExternalLinkIcon/></a></li>
<li><a href="https://cloud.tencent.com/developer/article/1699886" target="_blank" rel="noopener noreferrer">https://cloud.tencent.com/developer/article/1699886<ExternalLinkIcon/></a></li>
</ul>
</li>
</ul>
<p>在<code v-pre>Netty</code>中，<code v-pre>SO_BACKLOG</code>主要用于设置全连接队列的大小。当处理<code v-pre>Accept</code>的速率小于连接建立的速率时，全连接队列中堆积的连接数大于<code v-pre>SO_BACKLOG</code>设置的值时，便会抛出异常，设置方式如下：</p>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token comment">// 设置全连接队列，大小为2</span>
<span class="token keyword">new</span> <span class="token class-name">ServerBootstrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">option</span><span class="token punctuation">(</span><span class="token class-name">ChannelOption</span><span class="token punctuation">.</span><span class="token constant">SO_BACKLOG</span><span class="token punctuation">,</span> <span class="token number">2</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><blockquote>
<p>如何进行演示？提供一种思路就是在<code v-pre>Netty</code>的源码中进行<code v-pre>accept</code>的时候直接打上断点，让其无法向下执行，此时全连接队列满，客户端再次连接服务器就会抛出一个拒绝连接的异常。</p>
</blockquote>
<p>如果不指定的话，<code v-pre>Netty</code>会给一个默认值，那么怎么找到这个默认值呢？</p>
<p>首先说明：<code v-pre>backlog</code>参数在<code v-pre>NioServerSocketChannel.doBind</code>方法被使用</p>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token annotation punctuation">@Override</span>
<span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">doBind</span><span class="token punctuation">(</span><span class="token class-name">SocketAddress</span> localAddress<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">Exception</span> <span class="token punctuation">{</span>
    <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">PlatformDependent</span><span class="token punctuation">.</span><span class="token function">javaVersion</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">>=</span> <span class="token number">7</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
      <span class="token comment">// 使用到了一个默认的backlog参数</span>
        <span class="token function">javaChannel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>localAddress<span class="token punctuation">,</span> config<span class="token punctuation">.</span><span class="token function">getBacklog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        <span class="token function">javaChannel</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">socket</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">bind</span><span class="token punctuation">(</span>localAddress<span class="token punctuation">,</span> config<span class="token punctuation">.</span><span class="token function">getBacklog</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token comment">// 接口</span>
<span class="token keyword">private</span> <span class="token keyword">final</span> <span class="token class-name">ServerSocketChannelConfig</span> config<span class="token punctuation">;</span>
<span class="token comment">// ServerSocketChannelConfig是一个接口</span>
<span class="token keyword">public</span> <span class="token keyword">interface</span> <span class="token class-name">ServerSocketChannelConfig</span> <span class="token keyword">extends</span> <span class="token class-name">ChannelConfig</span><span class="token punctuation">{</span>
  <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
<span class="token comment">// 找到其实现类</span>
<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DefaultServerSocketChannelConfig</span> <span class="token keyword">extends</span> <span class="token class-name">DefaultChannelConfig</span>
                                              <span class="token keyword">implements</span> <span class="token class-name">ServerSocketChannelConfig</span> <span class="token punctuation">{</span>
  <span class="token punctuation">.</span><span class="token punctuation">.</span><span class="token punctuation">.</span>
<span class="token punctuation">}</span>
<span class="token comment">// 在实现类中找到实现的getBacklog方法即可</span>
<span class="token keyword">private</span> <span class="token keyword">volatile</span> <span class="token keyword">int</span> backlog <span class="token operator">=</span> <span class="token class-name">NetUtil</span><span class="token punctuation">.</span><span class="token constant">SOMAXCONN</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token comment">// As a SecurityManager may prevent reading the somaxconn file we wrap this in a privileged block.</span>
<span class="token comment">//</span>
<span class="token comment">// See https://github.com/netty/netty/issues/3680</span>
<span class="token constant">SOMAXCONN</span> <span class="token operator">=</span> <span class="token class-name">AccessController</span><span class="token punctuation">.</span><span class="token function">doPrivileged</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">PrivilegedAction</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Integer</span><span class="token punctuation">></span></span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token annotation punctuation">@Override</span>
    <span class="token keyword">public</span> <span class="token class-name">Integer</span> <span class="token function">run</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token comment">// Determine the default somaxconn (server socket backlog) value of the platform.</span>
        <span class="token comment">// The known defaults:</span>
        <span class="token comment">// - Windows NT Server 4.0+: 200</span>
        <span class="token comment">// - Linux and Mac OS X: 128</span>
        <span class="token keyword">int</span> somaxconn <span class="token operator">=</span> <span class="token class-name">PlatformDependent</span><span class="token punctuation">.</span><span class="token function">isWindows</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token number">200</span> <span class="token operator">:</span> <span class="token number">128</span><span class="token punctuation">;</span>
        <span class="token class-name">File</span> file <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">File</span><span class="token punctuation">(</span><span class="token string">"/proc/sys/net/core/somaxconn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token class-name">BufferedReader</span> in <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
        <span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token comment">// file.exists() may throw a SecurityException if a SecurityManager is used, so execute it in the</span>
            <span class="token comment">// try / catch block.</span>
            <span class="token comment">// See https://github.com/netty/netty/issues/4936</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>file<span class="token punctuation">.</span><span class="token function">exists</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                in <span class="token operator">=</span> <span class="token keyword">new</span> <span class="token class-name">BufferedReader</span><span class="token punctuation">(</span><span class="token keyword">new</span> <span class="token class-name">FileReader</span><span class="token punctuation">(</span>file<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
              <span class="token comment">// 如果在系统文件中找到就用系统文件的somaxconn，即全连接队列的大小</span>
                somaxconn <span class="token operator">=</span> <span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">parseInt</span><span class="token punctuation">(</span>in<span class="token punctuation">.</span><span class="token function">readLine</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span>logger<span class="token punctuation">.</span><span class="token function">isDebugEnabled</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    logger<span class="token punctuation">.</span><span class="token function">debug</span><span class="token punctuation">(</span><span class="token string">"{}: {}"</span><span class="token punctuation">,</span> file<span class="token punctuation">,</span> somaxconn<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                <span class="token comment">// Try to get from sysctl</span>
                <span class="token class-name">Integer</span> tmp <span class="token operator">=</span> <span class="token keyword">null</span><span class="token punctuation">;</span>
                <span class="token keyword">if</span> <span class="token punctuation">(</span><span class="token class-name">SystemPropertyUtil</span><span class="token punctuation">.</span><span class="token function">getBoolean</span><span class="token punctuation">(</span><span class="token string">"io.netty.net.somaxconn.trySysctl"</span><span class="token punctuation">,</span> <span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    tmp <span class="token operator">=</span> <span class="token function">sysctlGetInt</span><span class="token punctuation">(</span><span class="token string">"kern.ipc.somaxconn"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                    <span class="token keyword">if</span> <span class="token punctuation">(</span>tmp <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                        tmp <span class="token operator">=</span> <span class="token function">sysctlGetInt</span><span class="token punctuation">(</span><span class="token string">"kern.ipc.soacceptqueue"</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                        <span class="token keyword">if</span> <span class="token punctuation">(</span>tmp <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                            somaxconn <span class="token operator">=</span> tmp<span class="token punctuation">;</span>
                        <span class="token punctuation">}</span>
                    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
                        somaxconn <span class="token operator">=</span> tmp<span class="token punctuation">;</span>
                    <span class="token punctuation">}</span>
                <span class="token punctuation">}</span>

                <span class="token keyword">if</span> <span class="token punctuation">(</span>tmp <span class="token operator">==</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    logger<span class="token punctuation">.</span><span class="token function">debug</span><span class="token punctuation">(</span><span class="token string">"Failed to get SOMAXCONN from sysctl and file {}. Default: {}"</span><span class="token punctuation">,</span> file<span class="token punctuation">,</span>
                                 somaxconn<span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            logger<span class="token punctuation">.</span><span class="token function">debug</span><span class="token punctuation">(</span><span class="token string">"Failed to get SOMAXCONN from sysctl and file {}. Default: {}"</span><span class="token punctuation">,</span> file<span class="token punctuation">,</span> somaxconn<span class="token punctuation">,</span> e<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span> <span class="token keyword">finally</span> <span class="token punctuation">{</span>
            <span class="token keyword">if</span> <span class="token punctuation">(</span>in <span class="token operator">!=</span> <span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
                <span class="token keyword">try</span> <span class="token punctuation">{</span>
                    in<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
                <span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
                    <span class="token comment">// Ignored.</span>
                <span class="token punctuation">}</span>
            <span class="token punctuation">}</span>
        <span class="token punctuation">}</span>
      <span class="token comment">// 返回somaxconn</span>
        <span class="token keyword">return</span> somaxconn<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
<span class="token punctuation">}</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><p>从上面的源码中可以看出，<code v-pre>backlog</code>的值会根据操作系统的不同，来选择不同的默认值</p>
<ul>
<li><code v-pre>Windows 200</code></li>
<li><code v-pre>Linux/Mac OS 128</code></li>
<li>如果配置文件<code v-pre>/proc/sys/net/core/somaxconn</code>存在，会读取配置文件中的值，并将<code v-pre>backlog</code>的值设置为配置文件中指定的</li>
</ul>
<p><strong><code v-pre>TCP_NODELAY</code></strong></p>
<ul>
<li>属于 <code v-pre>SocketChannal</code> 参数</li>
<li>因为 <code v-pre>Nagle</code> 算法，数据包会堆积到一定的数量后一起发送，这就可能导致数据的发送存在一定的延时</li>
<li>该参数默认为<code v-pre>false</code>，也就是默认启用了<code v-pre>Nagle</code>算法，如果不希望发送被延时，则需要将该值设置为<code v-pre>true</code></li>
</ul>
<p><strong><code v-pre>SO_SNDBUF &amp; SO_RCVBUF</code></strong></p>
<ul>
<li><code v-pre>SO_SNDBUF</code> 属于 <code v-pre>SocketChannal</code> 参数</li>
<li><code v-pre>SO_RCVBUF</code> 既可用于 <code v-pre>SocketChannal</code> 参数，也可以用于 <code v-pre>ServerSocketChannal</code> 参数（建议设置到 <code v-pre>ServerSocketChannal</code> 上）</li>
<li>该参数用于指定接收方与发送方的滑动窗口大小</li>
</ul>
<p><strong><code v-pre>ALLOCATOR</code></strong></p>
<ul>
<li>属于 <code v-pre>SocketChannal</code> 参数</li>
<li>用来配置 <code v-pre>ByteBuf</code> 是池化还是非池化，是直接内存还是堆内存</li>
</ul>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token comment">// 选择ALLOCATOR参数，设置SocketChannel中分配的ByteBuf类型</span>
<span class="token comment">// 第二个参数需要传入一个ByteBufAllocator，用于指定生成的 ByteBuf 的类型</span>
<span class="token keyword">new</span> <span class="token class-name">ServerBootstrap</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">childOption</span><span class="token punctuation">(</span><span class="token class-name">ChannelOption</span><span class="token punctuation">.</span><span class="token constant">ALLOCATOR</span><span class="token punctuation">,</span> <span class="token keyword">new</span> <span class="token class-name">PooledByteBufAllocator</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div><div class="line-number"></div></div></div><ul>
<li>
<p><code v-pre>ByteBufAllocator</code>类型</p>
<ul>
<li>池化并使用直接内存</li>
</ul>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token comment">// true表示使用直接内存</span>
<span class="token keyword">new</span> <span class="token class-name">PooledByteBufAllocator</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><ul>
<li>池化并使用堆内存</li>
</ul>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token comment">// false表示使用堆内存</span>
<span class="token keyword">new</span> <span class="token class-name">PooledByteBufAllocator</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><ul>
<li>非池化并使用直接内存</li>
</ul>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token comment">// ture表示使用直接内存</span>
<span class="token keyword">new</span> <span class="token class-name">UnpooledByteBufAllocator</span><span class="token punctuation">(</span><span class="token boolean">true</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div><ul>
<li>非池化并使用堆内存</li>
</ul>
<div class="language-java line-numbers-mode" data-ext="java"><pre v-pre class="language-java"><code><span class="token comment">// false表示使用堆内存</span>
<span class="token keyword">new</span> <span class="token class-name">UnpooledByteBufAllocator</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
</code></pre><div class="line-numbers" aria-hidden="true"><div class="line-number"></div><div class="line-number"></div></div></div></li>
</ul>
<p><strong><code v-pre>RCVBUF_ALLOCATOR</code></strong></p>
<ul>
<li>属于 <code v-pre>SocketChannal</code> 参数</li>
<li>控制 <code v-pre>Netty</code> 接收缓冲区大小</li>
<li>负责入站数据的分配，决定入站缓冲区的大小（并可动态调整），统一采用 <code v-pre>direct</code> 直接内存，具体池化还是非池化由 <code v-pre>allocator</code> 决定</li>
</ul>
<h3 id="_3、rpc框架" tabindex="-1"><a class="header-anchor" href="#_3、rpc框架" aria-hidden="true">#</a> 3、RPC框架</h3>
<h3 id="_4、项目代码" tabindex="-1"><a class="header-anchor" href="#_4、项目代码" aria-hidden="true">#</a> 4、项目代码</h3>
<blockquote>
<p><a href="https://gitee.com/oucystong/netty-chat.git" target="_blank" rel="noopener noreferrer">https://gitee.com/oucystong/netty-chat.git<ExternalLinkIcon/></a></p>
</blockquote>
<h2 id="二、源码" tabindex="-1"><a class="header-anchor" href="#二、源码" aria-hidden="true">#</a> 二、源码</h2>
<h2 id="三、参考" tabindex="-1"><a class="header-anchor" href="#三、参考" aria-hidden="true">#</a> 三、参考</h2>
<ul>
<li><a href="https://nyimac.gitee.io/2021/04/25/Netty%E5%9F%BA%E7%A1%80/" target="_blank" rel="noopener noreferrer">https://nyimac.gitee.io/2021/04/25/Netty基础/<ExternalLinkIcon/></a></li>
<li><a href="https://www.bilibili.com/video/BV1py4y1E7oA/" target="_blank" rel="noopener noreferrer">https://www.bilibili.com/video/BV1py4y1E7oA/<ExternalLinkIcon/></a></li>
</ul>
</div></template>


